﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
using DXWebApplication.Areas.Administrators.Models.News;
using DXWebApplication.Repositories.Core.News;
using DXWebApplication.Repositories.Implementations.News;
using Newtonsoft.Json;

namespace DXWebApplication.Areas.Administrators.Controllers.News
{
    public class NewsLogController : AdminBaseController, IAdminBaseController<NewsLogModel>
    {
          private readonly INewsLogRepository _newsLogRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="NewsLogController" /> class.
        /// </summary>
          public NewsLogController()
        {
            _newsLogRepository = new NewsLogRepository();
        }
        #region method

        /// <summary>
        /// Inserts this instance.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public PartialViewResult Insert()
        {
            var result = new NewsLogModel();
            return PartialView("NewsLogDetail", result);
        }

        /// <summary>
        /// Inserts the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Insert(NewsLogModel model)
        {
            var msg = string.Empty;
            if (ModelState.IsValid)
            {
                msg = _newsLogRepository.Insert(model.FromModel());
            }
            return Json(new { message = msg });
        }

        /// <summary>
        /// Updates this instance.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        [HttpGet]
        public PartialViewResult Update(string idJson)
        {
            var idList = JsonConvert.DeserializeObject<List<int>>(idJson);
            var model = new NewsLogModel();
            if (idList.Count == 1)
            {
                int id;
                int.TryParse(idList[0].ToString(CultureInfo.InvariantCulture), out id);
                if (id > 0)
                {
                    model = _newsLogRepository.Get(id).ToModel();
                }
            }
            return PartialView("NewsLogDetail", model);
        }

        /// <summary>
        /// Updates the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Update(NewsLogModel model)
        {
            var msg = string.Empty;
            if (ModelState.IsValid)
            {                
                msg = _newsLogRepository.Update(model.FromModel());
            }
            return Json(new { message = msg });
        }

        /// <summary>
        /// Deletes the specified ids.
        /// </summary>
        /// <param name="idJson">The identifier json.</param>
        /// <returns></returns>
        public JsonResult Delete(string idJson)
        {
            string msg;
            try
            {
                var idList = JsonConvert.DeserializeObject<IList<int>>(idJson);
                msg = _newsLogRepository.Delete(idList);
            }
            catch (Exception)
            {
                return Json(new { message = NmResources.MessageResource.Exception }, JsonRequestBehavior.AllowGet);
            }
            return Json(new { message = msg }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        [HttpGet]
        public PartialViewResult GetPaged(int currentPage = 1)
        {
            var totalPage = 0;
            var result = _newsLogRepository.GetPaged(ref totalPage, currentPage);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            return PartialView("NewsLog",result.ToModel());
        }

     
        #endregion
        
    }
}
